<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java中的无锁编程</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            padding: 16px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({ startOnLoad: true, theme: 'default' });
    </script>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">Java中的无锁编程</h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">探索高并发编程的艺术与科学</p>
                <div class="flex justify-center space-x-4">
                    <div class="flex items-center">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>高性能</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>线程安全</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>无锁算法</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>现代高并发编程中，"无锁编程"或"Lock-Free编程"逐渐成为提升性能的一个关键手段。传统的并发控制通常依赖锁（如<code class="bg-gray-100 px-2 py-1 rounded">ReentrantLock</code>和<code class="bg-gray-100 px-2 py-1 rounded">synchronized</code>），这些锁机制通过限制资源的同时访问，确保了数据一致性和线程安全。然而，锁本身会带来开销，尤其是在高并发环境下，锁竞争、线程阻塞和上下文切换会导致性能下降。
            </p>
        </div>

        <!-- Key Concepts Visualization -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">核心概念关系图</h2>
            <div class="mermaid">
                graph TD
                    A[无锁编程] --> B[CAS操作]
                    A --> C[原子类]
                    A --> D[无锁数据结构]
                    B --> E[AtomicInteger]
                    B --> F[AtomicReference]
                    C --> G[内存屏障]
                    C --> H[volatile关键字]
                    D --> I[ConcurrentLinkedQueue]
                    D --> J[ConcurrentHashMap]
                    D --> K[ConcurrentSkipListMap]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- What is Lock-Free Programming -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">什么是无锁编程</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                无锁编程（Lock-Free Programming）是一种在多线程环境下实现数据结构和算法的并发控制技术。它的核心思想是不使用传统的锁机制（如synchronized和ReentrantLock），而是通过乐观并发控制，让多个线程能够独立并行地操作共享数据而不发生阻塞。
            </p>
            
            <div class="grid md:grid-cols-3 gap-6 mt-8">
                <div class="feature-card">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">非阻塞特性</h3>
                    <p class="text-gray-600">线程不会因无法获取锁而阻塞，通过CAS操作重试更新，减少线程切换开销。</p>
                </div>
                <div class="feature-card">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-unlock"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">避免死锁</h3>
                    <p class="text-gray-600">无锁编程中没有锁的存在，天然避免了死锁、活锁和锁饥饿等问题。</p>
                </div>
                <div class="feature-card">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-users"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">支持高并发</h3>
                    <p class="text-gray-600">允许多个线程同时操作共享数据，在高并发场景中显著提升系统吞吐量。</p>
                </div>
            </div>
        </div>

        <!-- CAS Operation -->
        <div class="highlight-box">
            <h3 class="text-2xl font-bold mb-4"><i class="fas fa-key mr-2"></i>无锁编程的核心技术：CAS</h3>
            <p class="text-lg mb-4">
                CAS（Compare-And-Swap）是一种原子操作，通过在修改前检查数据是否处于预期的状态，确保只有在没有其他线程干扰的情况下才会更新数据。
            </p>
            <div class="bg-white bg-opacity-20 rounded-lg p-4">
                <p class="font-semibold mb-2">CAS操作涉及三个参数：</p>
                <ul class="space-y-2">
                    <li><i class="fas fa-map-marker-alt mr-2"></i><strong>内存位置</strong>：需要修改的数据的位置</li>
                    <li><i class="fas fa-eye mr-2"></i><strong>预期值</strong>：当前线程希望该位置的值</li>
                    <li><i class="fas fa-edit mr-2"></i><strong>新值</strong>：如果位置的值和预期值相同，则替换为新值</li>
                </ul>
            </div>
        </div>

        <!-- Implementation Methods -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8 mt-8">
            <h2 class="text-3xl font-bold mb-6 section-title">无锁编程的实现方式</h2>
            
            <div class="space-y-6">
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-2">1. 基于CAS的无锁操作</h3>
                    <p class="text-gray-700">CAS操作是无锁编程的基础，允许线程在执行数据更新时，通过比较并交换的方式确保操作的原子性。</p>
                </div>
                
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-2">2. 原子类（Atomic Classes）实现</h3>
                    <p class="text-gray-700">Java提供了基于CAS的原子类，如<code class="bg-gray-100 px-2 py-1 rounded">AtomicInteger</code>、<code class="bg-gray-100 px-2 py-1 rounded">AtomicLong</code>、<code class="bg-gray-100 px-2 py-1 rounded">AtomicReference</code>等。</p>
                </div>
                
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-2">3. 无锁数据结构</h3>
                    <p class="text-gray-700">如<code class="bg-gray-100 px-2 py-1 rounded">ConcurrentLinkedQueue</code>、<code class="bg-gray-100 px-2 py-1 rounded">ConcurrentSkipListMap</code>等，通过CAS实现线程安全的数据存取。</p>
                </div>
                
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-2">4. 版本号和标记机制</h3>
                    <p class="text-gray-700">使用<code class="bg-gray-100 px-2 py-1 rounded">AtomicStampedReference</code>解决ABA问题，通过版本号确保数据的真正一致性。</p>
                </div>
            </div>
        </div>

        <!-- Lock-Free Data Structures -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">无锁数据结构</h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3"><i class="fas fa-list mr-2 text-purple-600"></i>ConcurrentLinkedQueue</h3>
                    <p class="text-gray-700">基于无锁链表的FIFO队列，采用Michael & Scott无锁队列算法，通过CAS操作维护头尾节点。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3"><i class="fas fa-layer-group mr-2 text-blue-600"></i>ConcurrentSkipListMap</h3>
                    <p class="text-gray-700">基于跳表实现的并发有序映射